package com.ds.home.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ds.home.EIDevice;
import com.ds.home.EIDeviceEndPoint;
import com.ds.home.EISensortype;
import com.ds.home.database.vo.DeviceVO;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Device entity. @author MyEclipse Persistence Tools
 */

public class DBDevice implements EIDevice {

    // Fields
    private DeviceVO devicevo;


    /**
     * full constructor
     */
    protected DBDevice(DeviceVO vo) {

        devicevo = vo;

    }

    public Long getAddtime() {
        return this.devicevo.getAddtime();
    }

    public String getAppaccount() {

        return this.devicevo.getAppaccount();
    }

    public String getBatch() {
        return this.devicevo.getBatch();
    }

    public String getBattery() {
        return this.devicevo.getBattery();
    }

    public String getBindingaccount() {
        return this.devicevo.getBindingaccount();
    }

    public String getChannel() {
        return this.devicevo.getChannel();
    }

    public String getDeviceid() {
        return this.devicevo.getDeviceid();
    }

    public Integer getDevicetype() {
        return this.devicevo.getDevicetype();
    }

    public String getFactory() {
        return this.devicevo.getFactory();
    }

    public String getMacaddress() {
        return this.devicevo.getMacaddress();
    }

    public String getName() {
        return this.devicevo.getName();
    }

    public String getSerialno() {
        return this.devicevo.getSerialno();
    }

    public Integer getStates() {
        return this.devicevo.getStates();
    }

    public String getSubsyscode() {
        return this.devicevo.getSubsyscode();
    }

    public void setAddtime(Long addtime) {
        this.devicevo.setAddtime(addtime);
    }

    public void setAppaccount(String appaccount) {

        this.devicevo.setAppaccount(appaccount);
    }

    public void setBatch(String batch) {
        this.devicevo.setBatch(batch);
    }

    public void setBattery(String battery) {
        this.devicevo.setBattery(battery);
    }

    public void setBindingaccount(String bindingaccount) {
        this.devicevo.setBindingaccount(bindingaccount);
    }

    public void setChannel(String channel) {
        this.devicevo.setChannel(channel);
    }

    public void setDevicetype(Integer devicetype) {
        this.devicevo.setDevicetype(devicetype);
    }

    public void setFactory(String factory) {
        this.devicevo.setFactory(factory);
    }

    public void setMacaddress(String macaddress) {
        this.devicevo.setMacaddress(macaddress);
    }

    public void setName(String name) {
        this.devicevo.setName(name);
    }

    public void setSerialno(String serialno) {
        this.devicevo.setSerialno(serialno);
    }

    public void setStates(Integer states) {

        this.devicevo.setStates(states);
    }

    public void setSubsyscode(String subsyscode) {
        this.devicevo.setSubsyscode(subsyscode);
    }


    @Override
    public String getAreaid() {
        return this.devicevo.getAreaid();
    }

    @Override
    public void setAreaid(String areaid) {
        this.devicevo.setAreaid(areaid);
    }

    @Override
    public String getPlaceid() {
        return this.devicevo.getPlaceid();
    }

    @Override
    public void setPlaceid(String placeid) {
        devicevo.setPlaceid(placeid);
    }

    public EISensortype getSensortype() {
        EISensortype sensortype = MEMManagerImpl.getInstance().getSensoTypeByNo(this.devicevo.getDevicetype());
        return sensortype;
    }

    public EIDeviceEndPoint addEndPoint(EIDeviceEndPoint endpoint) {
        Set enpoindIds = getDeviceEndPointIds();
        if (!enpoindIds.contains(endpoint.getEndPointId())) {
            enpoindIds.add(endpoint.getEndPointId());
            MEMManagerImpl.getInstance().getAllEPIdCache().put(this.getDeviceid(), JSONObject.toJSONString(enpoindIds));

        }
        return endpoint;
    }

    public void removeEndPoint(String endPointId) {
        Set enpoindIds = getDeviceEndPointIds();
        if (enpoindIds.contains(endPointId)) {
            enpoindIds.remove(endPointId);
            MEMManagerImpl.getInstance().getAllEPIdCache().put(this.getDeviceid(), JSONObject.toJSONString(enpoindIds));
        }

    }

    public List<EIDeviceEndPoint> getDeviceEndPoints() {

        List<EIDeviceEndPoint> enpoinds = new ArrayList<EIDeviceEndPoint>();
        Set<String> enpoindIds = getDeviceEndPointIds();
        for (String epId : enpoindIds) {

            EIDeviceEndPoint endPoint = MEMManagerImpl.getInstance().getEndPoindById(epId);
            if (endPoint != null) {
                enpoinds.add(endPoint);
            }

        }
        return enpoinds;
    }


    public EIDevice addChildDevice(EIDevice device) {
        Set deviceIds = getChildDeviceIds();

        if (!deviceIds.contains(device.getDeviceid()) && !device.isRoot()) {
            deviceIds.add(device.getDeviceid());
            MEMManagerImpl.getInstance().getAllChildIdCache().put(this.getDeviceid(), JSONObject.toJSONString(deviceIds));

        }
        return device;
    }

    public void removeChildDevice(String deviceId) {
        Set deviceIds = getChildDeviceIds();
        if (deviceIds.contains(deviceId)) {
            deviceIds.remove(deviceId);
            MEMManagerImpl.getInstance().getAllChildIdCache().put(this.getDeviceid(), JSONObject.toJSONString(deviceIds));
        }

    }

    public List<EIDevice> getChildDevices() {
        List<EIDevice> childDevices = new ArrayList<EIDevice>();
        Set<String> childIds = getChildDeviceIds();
        for (String deviceId : childIds) {
            EIDevice device = MEMManagerImpl.getInstance().getDeviceById(deviceId);
            childDevices.add(device);
        }
        return childDevices;
    }

    public Set<String> getChildDeviceIds() {
        Set<String> deviceIds = new LinkedHashSet<String>();
        if (this.isRoot()) {
            List<EIDevice> devices = MEMManagerImpl.getInstance().getDevicesByBindAccount(this.getBindingaccount());
            for (EIDevice device : devices) {
                if (!device.getDeviceid().equals(device.getBindingaccount())) {
                    //if (!device.getDeviceid().equals(device.getBindingaccount()) && !device.getStates().equals(DeviceStatus.DELETE.getType())) {
                    deviceIds.add(device.getDeviceid());
                }
            }
//
//
//            String idsjson = MEMManagerImpl.getInstance().getAllChildIdCache().get(this.getDeviceid());
//            if (idsjson == null) {
//                List<EIDevice> devices = MEMManagerImpl.getInstance().getDevicesByBindAccount(this.getBindingaccount());
//                for (EIDevice device : devices) {
//                    if (!device.getDeviceid().equals(device.getBindingaccount())) {
//                        //if (!device.getDeviceid().equals(device.getBindingaccount()) && !device.getStates().equals(DeviceStatus.DELETE.getType())) {
//                        deviceIds.add(device.getDeviceid());
//                    }
//                }
//                idsjson = JSONObject.toJSONString(deviceIds);
//                //  MEMManagerImpl.getInstance().getAllChildIdCache().put(this.getDeviceid(), JSONObject.toJSONString(deviceIds));
//            }
//            deviceIds = new LinkedHashSet<String>(JSONArray.parseArray(idsjson, String.class));
        }


        return deviceIds;
    }

    public EIDevice getRootDevice() {
        return MEMManagerImpl.getInstance().getDeviceById(this.getBindingaccount());
    }

    public boolean isRoot() {
        return this.getDeviceid().equals(this.getBindingaccount());
    }

    public DeviceVO getDevicevo() {
        return devicevo;
    }

    public void setDevicevo(DeviceVO devicevo) {
        this.devicevo = devicevo;
    }

    @Override
    public Set<String> getDeviceEndPointIds() {
        String idsjson = MEMManagerImpl.getInstance().getAllEPIdCache().get(this.getDeviceid());
        Set<String> enpoindIds = new LinkedHashSet<String>();
        if (idsjson == null) {
            List<EIDeviceEndPoint> eps = MEMManagerImpl.getInstance().getEndPointsByDeviceId(this.getDeviceid());
            for (EIDeviceEndPoint ep : eps) {
                enpoindIds.add(ep.getEndPointId());
            }
            idsjson = JSONObject.toJSONString(enpoindIds);
            MEMManagerImpl.getInstance().getAllEPIdCache().put(this.getDeviceid(), JSONObject.toJSONString(enpoindIds));
        }
        enpoindIds = new LinkedHashSet<String>(JSONArray.parseArray(idsjson, String.class));
        return enpoindIds;
    }

    public Long getLastlogintime() {
        return devicevo.getLastlogintime();
    }

    public void setLastlogintime(Long lastlogintime) {
        devicevo.setLastlogintime(lastlogintime);
    }


}